﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;


namespace groep20_festival
{
    public partial class roles : System.Web.UI.Page
    {
        /// <summary>
        /// controle of de pagina voor het eerste keer bezocht wordt
        /// jquery voor validatie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
                DisplayRolesInGrid();

            ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
            {

                Path = "~/scripts/jquery-1.4.1.min.js",
                DebugPath = "~/scripts/jquery-1.4.1.js",
                CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
                CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
            });
        }

        /// <summary>
        /// ophalen van roles
        /// </summary>
        private void DisplayRolesInGrid()
        {
            RoleList.DataSource = Roles.GetAllRoles();
            RoleList.DataBind();
        }

        /// <summary>
        /// creeren van een role
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreateRoleButton_Click(object sender, EventArgs e)
        {
            string newRoleName = RoleName.Text.Trim();

            if (!Roles.RoleExists(newRoleName))
            {
                
                // Create the role
                Roles.CreateRole(newRoleName);

                // Refresh the RoleList Grid
                DisplayRolesInGrid();
            }

            RoleName.Text = string.Empty;
        }

        /// <summary>
        /// verwijderen van een role
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RoleList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            // Get the RoleNameLabel
            Label RoleNameLabel = RoleList.Rows[e.RowIndex].FindControl("RoleNameLabel") as Label;

            // Delete the role
            Roles.DeleteRole(RoleNameLabel.Text, false);

            // Rebind the data to the RoleList grid
            DisplayRolesInGrid();
        }

      
    }
}
